package com.zhwmall.gateway.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author:yanjizhw@163.com
 * @date:2020/8/21 15:14
 * 描述: 全局配置跨域
 * @since:1.0.0
 */
@Configuration
public class CORSConfig {

    /**
     * @return 跨域配置
     */
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); //允许任何域名
        corsConfiguration.addAllowedHeader("*"); //允许任何头
        corsConfiguration.addAllowedMethod("*"); //允许任何方法
        corsConfiguration.setAllowCredentials(true); // 允许携带cookie进行跨域
        return corsConfiguration;
    }

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); //注册。参数1：请求的路径  参数2：跨域配置
        return new CorsWebFilter(source);
    }
}
